package com.probox.modules.controller.platfom;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.platfom.PlatformStore;
import com.probox.common.entity.mapper.system.SysUserMapper;
import com.probox.common.entity.service.common.SysReginService;
import com.probox.common.entity.service.platfom.IPlatformStoreService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 门店信息Controller
 *
 * @author usoft
 * @date 2021-08-20
 */
@RestController
@RequestMapping("/platform/store")
public class PlatformStoreController extends BaseController {
    @Autowired
    private IPlatformStoreService platformStoreService;

    @Autowired
    private SysReginService sysReginService;

    @Autowired
    private SysUserMapper userMapper ;


    @GetMapping("/selectGroup")
    public AjaxResult selectGroup() {
        return AjaxResult.success(platformStoreService.selectGroup());
    }
    //未分组门店筛选
    @GetMapping("/listSelectGroup")
    public AjaxResult listSelectGroup() {
        return AjaxResult.success(platformStoreService.listSelectGroup(SecurityUtils.getPlatformId()));
    }

    @GetMapping("/listSelect")
    public AjaxResult listSelect() {
        return AjaxResult.success(platformStoreService.listSelect(SecurityUtils.getPlatformId()));
    }


    //用户管理门店
    @GetMapping("/listSelectSelf")
    public AjaxResult listSelectSelf() {
        return AjaxResult.success(platformStoreService.listSelectSelf(SecurityUtils.getPlatformId()));
    }

    //推荐客户门店筛选
    @GetMapping("/listRecommend")
    public AjaxResult listRecommend() {
        return AjaxResult.success(platformStoreService.listRecommend());
    }

    /**
     * 查询门店信息列表
     */
    @PreAuthorize(hasPermi = "platform:store:list")
    @GetMapping("/list")
    public TableDataInfo list(PlatformStore platformStore) {
        if(ObjectUtil.isNotEmpty(SecurityUtils.getStoreId())){
            platformStore.setStoreUid(SecurityUtils.getStoreId());
        }
        startPage();
        List<PlatformStore> list = platformStoreService.queryList(platformStore);
//        if (ObjectUtil.isEmpty(list)) return getDataTable(list);
//        List<PlatformStoreVo> listVo = new ArrayList<>();
//        list.forEach(store -> {
//            PlatformStoreVo vo = new PlatformStoreVo();
//            BeanUtils.copyProperties(store, vo);
//            listVo.add(vo);
//        });
        return getDataTable(list);
    }




    /**
     * 导出门店信息列表
     */
    @PreAuthorize(hasPermi = "platform:store:export")
    @Log(title = "门店信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlatformStore platformStore) throws IOException {
        List<PlatformStore> list = platformStoreService.queryList(platformStore);
        ExcelUtil<PlatformStore> util = new ExcelUtil<PlatformStore>(PlatformStore.class);
        util.exportExcel(response, list, "门店信息数据");
    }

    /**
     * 获取门店信息详细信息
     */
    @PreAuthorize(hasPermi = "platform:store:query")
    @GetMapping(value = "/{storeUid}")
    public AjaxResult getInfo(@PathVariable("storeUid") Long storeUid) {
        String[] labels = new String[3];
        PlatformStore platformStore = platformStoreService.selectPlatformStoreById(storeUid);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("data", platformStore);
        if (ObjectUtil.isNotEmpty(platformStore)) {
            if (ObjectUtil.isNotEmpty(platformStore.getStoreProvince())) {
                labels[0] = platformStore.getStoreProvince();
                labels[1] = platformStore.getStoreCity();
                labels[2] = platformStore.getStoreArea();
            }
        }
        ajaxResult.put("region", sysReginService.getReginCode(labels));
        return ajaxResult;
    }

    /**
     * 新增门店信息
     */
    @PreAuthorize(hasPermi = "platform:store:add")
    @Log(title = "门店信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformStore platformStore) {
        return toAjax(platformStoreService.insertPlatformStore(platformStore));
    }

    /**
     * 修改门店信息
     */
    @PreAuthorize(hasPermi = "platform:store:edit")
    @Log(title = "门店信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformStore platformStore) {
        return toAjax(platformStoreService.updatePlatformStore(platformStore));
    }



    /**
     * 删除门店信息
     */
    @PreAuthorize(hasPermi = "platform:store:remove")
    @Log(title = "门店信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeUids}")
    public AjaxResult remove(@PathVariable Long[] storeUids) {
        return toAjax(platformStoreService.deletePlatformStoreByIds(storeUids));
    }



    @GetMapping("/selectByUser/{storeId}")
    public AjaxResult selectByUser(@PathVariable Long storeId) {
        return AjaxResult.success(platformStoreService.selectByUser(storeId));
    }

    @GetMapping("/selectNoGroup")
    public AjaxResult selectNoGroup(@RequestParam(value="uid",required = false) Long uid) {
        return AjaxResult.success(platformStoreService.selectNoGroup(uid));
    }

}
